// components/tabbar/index.ts
Component({

    /**
     * 组件的属性列表
     */
    properties: {
        active: {
            type: String,
            value: 'package'
        }
    },

    /**
     * 组件的初始数据
     */
    data: {},

    /**
     * 组件的方法列表
     */
    methods: {
        toPage(e: any) {
            let p = e.currentTarget.dataset.page;
            if (p === this.properties.active) {
                return;
            }

            let url = '';
            switch (p) {
                case "package":
                    url = '/pages/mall/index';
                    break;

                case "book":
                    url = "/pages/book/index/index";
                    break;

                case "me":
                    url = "/pages/me/index"
            }

            let pages = getCurrentPages();
            let currentPage = pages[pages.length - 1];
            let currentRoute = currentPage.route;
            if ('/'+currentRoute === url) {
                return;
            }

            // 如果在栈中找到了目标页面，就直接跳转到目标页面
            for (let i = 0; i < pages.length; i++) {
                let page = pages[i];
                console.log(page.route);
                if ('/'+page.route === url) {
                    wx.navigateBack({
                        delta: pages.length - i - 1
                    });
                    return;
                }
            }

            console.log('start navigate to ' + url);
            wx.navigateTo({
              url: url,
              fail: function(e: any) {
                console.log(e);
              }
            })
        }
    }
})
